'use strict';

const {
	wxapi
} = require('use-api');

const {
	Controller
} = require('uni-cloud-router');
const dbcmd = uniCloud.database().command;
const $ = dbcmd.aggregate;

module.exports = class MessageController extends Controller {
	// 获取微信菜单
	async wxTemplateGet() {
		let response = {
			code: 1,
			datas: null,
			msg: ''
		};

		const wxTemplateUrl =
			'https://api.weixin.qq.com/cgi-bin/template/get_all_private_template';

		let start = Date.now();
		// 获取微信 access_token
		const token = await wxapi.get_token();

		const res = await this.curl(wxTemplateUrl, {
			nestedQuerystring: true,
			method: 'get',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			dataType: 'json',
			data: {
				access_token: token
			}
		});

		response.datas = res.data.template_list;

		let end = Date.now();
		response.code = 0;
		response.msg = `耗时：${end - start}ms`;
		return response;
	}

	// 获取绑定二维码
	async wxQrcodeGet() {
		let response = {
			code: 1,
			datas: null,
			msg: ''
		};

		let {
			scenes
		} = this.ctx.data;

		let start = Date.now();

		const scene = scenes.join('_');

		const res = await wxapi.qrcode_create_temp(`usemall_bindmsg_${scene}`);

		response.datas = res;

		let end = Date.now();
		response.code = 0;
		response.msg = `耗时：${end - start}ms`;
		return response;
	}

	// 解绑
	async unbind() {
		let response = {
			code: 1,
			datas: null,
			msg: ''
		};

		let {
			row,
			item
		} = this.ctx.data;

		let start = Date.now();

		const res = await this.db.collection('usemall-message-type').doc(row._id).update({
			uids: dbcmd.pull(item.wx_openid['h5-weixin']),
			users: dbcmd.pull({
				openid: item.wx_openid['h5-weixin']
			})
		})

		response.datas = res;

		let end = Date.now();
		response.code = 0;
		response.msg = `耗时：${end - start}ms`;
		return response;
	}

	// 未读数
	async unread() {
		let response = {
			code: 1,
			datas: []
		};

		const start = Date.now();

		const uid = this.ctx.auth.uid;

		let where_obj = {
			type: 1,
			del_uids: dbcmd.neq(uid),
			to_uids: uid,
			read_uids: dbcmd.neq(uid)
		};

		let res = await this.db.collection('usemall-message')
			.aggregate()
			.match(where_obj)
			.group({
				_id: null,
				num: $.sum(1)
			})
			.end()

		const [data = {}] = res.data;
		response.datas = data;

		const end = Date.now();
		console.log(`耗时：${end - start}ms`);
		response.code = 0;
		response.duration = `耗时：${end - start}ms`;
		return response;
	}

	// 列表
	async list() {
		let response = {
			code: 1,
			datas: []
		};

		const start = Date.now();
		const uid = this.ctx.auth.uid;

		let where_obj = {
			type: 1,
			del_uids: dbcmd.neq(uid),
			to_uids: uid,
			read_uids: dbcmd.neq(uid)
		};

		let res = await this.db.collection('usemall-message')
			.aggregate()
			.match(where_obj)
			.sort({
				create_time: -1
			})
			.end()

		response.uid = uid;
		response.datas = res.data;

		const end = Date.now();
		console.log(`耗时：${end - start}ms`);
		response.code = 0;
		response.duration = `耗时：${end - start}ms`;
		return response;
	}

	// 已读
	async read() {
		let response = {
			code: 1,
			datas: []
		};

		const {
			id
		} = this.ctx.data;

		const start = Date.now();
		const uid = this.ctx.auth.uid;

		let res = await this.db.collection('usemall-message')
			.doc(id)
			.update({
				read_uids: dbcmd.addToSet(uid)
			});

		response.datas = res;

		const end = Date.now();
		console.log(`耗时：${end - start}ms`);
		response.code = 0;
		response.duration = `耗时：${end - start}ms`;
		return response;
	}

	// 全部已读
	async allRead() {
		let response = {
			code: 1,
			datas: []
		};

		const start = Date.now();
		const uid = this.ctx.auth.uid;
		
		let where_obj = {
			type: 1,
			del_uids: dbcmd.neq(uid),
			to_uids: uid,
			read_uids: dbcmd.neq(uid)
		};
		
		let res = await this.db.collection('usemall-message')
			.where(where_obj)
			.update({
				read_uids: dbcmd.addToSet(uid)
			});

		response.datas = res;

		const end = Date.now();
		console.log(`耗时：${end - start}ms`);
		response.code = 0;
		response.duration = `耗时：${end - start}ms`;
		return response;
	}
}
